/**
 * @Title: StroeIndexController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/21 15:21
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.yiqi.wsc.Ro.ItemInfoVO;
import cn.com.yiqi.wsc.Ro.SubjectItemVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.BaseQueryDO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.shop.ItemMapper;
import cn.com.yiqi.wsc.entity.shop.AppBanner;

import java.util.List;

import cn.com.yiqi.wsc.entity.shop.AppSubject;

import cn.com.yiqi.wsc.ro.ItemInfoSearchDto;
import cn.com.yiqi.wsc.service.banner.impl.BannerServiceImpl;
import cn.com.yiqi.wsc.service.base.BaseParaService;
import cn.com.yiqi.wsc.service.file.dataobject.QiniuConfig;
import cn.com.yiqi.wsc.service.item.ItemService;
import cn.com.yiqi.wsc.service.item.impl.ItemTypeServiceImpl;
import cn.com.yiqi.wsc.service.subject.impl.SubjectServiceImpl;
import cn.com.yiqi.wsc.util.GsonUtil;
import com.github.pagehelper.PageInfo;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 会员系统组-林威
 * @ClassName: StroeIndexController
 * Description: TODO
 * @date 2017/2/21 15:21
 */
@ResponseBody
@Controller
@RequestMapping("/api/store")
public class ApiStoreController {
    @Autowired
    BannerServiceImpl bannerService;

    @Autowired
    SubjectServiceImpl subjectService;

    @Autowired
    ItemTypeServiceImpl itemTypeService;

    @Autowired
    ItemService itemService;

    @Autowired
    QiniuConfig qiniuConfig;

    @Autowired
    ItemMapper itemDao;

    @Autowired
    BaseParaService baseParaService;

    /**
     * banner列表
     */
    @RequestMapping("/banner")
    public Result banner() {
        Result re = new Result();
        //banner轮番图
        List<AppBanner> bannerList = bannerService.findBanners(qiniuConfig.getURL());
        re.putData("list", bannerList);
        return ResultUtil.success(re);
    }

    /**
     * 专题列表
     */
    @RequestMapping("/subject")
    public Result subject() {
        Result re = new Result();

        List<AppSubject> subjectListTop = subjectService.findSubject(1);
        List<AppSubject> subjectListButtum = subjectService.findSubject(2);
        setUrl(subjectListTop);
        setUrl(subjectListButtum);
        re.putData("subjectListTop", subjectListTop);
        re.putData("subjectListButtum", subjectListButtum);
        return ResultUtil.success(re);
    }

    /**
     * search_ad加一
     * */
    @RequestMapping("/click")
    public Result addClick(@RequestBody CommonRO ro){
        if(null != ro.getId()){
            itemDao.addSearchAd(ro.getId());
        }
        return ResultUtil.success(new Result());
    }



    /**
     * 解析json并设置路径
     * */
    private void setUrlOne(AppSubject subject){
        String baseUrl = qiniuConfig.getURL() + "/";
        List<String> list = GsonUtil.getGson().fromJson(subject.getBannerUrl(), new TypeToken<List<String>>() {
        }.getType());
        for (int i = 0; i < list.size(); i++) {
            list.set(i, baseUrl + list.get(i));
        }
        subject.setUrl(list);
    }

    private void setUrl(List<AppSubject> subjectListButtum) {
        for (AppSubject subject : subjectListButtum) {
          setUrlOne(subject);
        }
    }


    /**
     * 专题详情
     */
    @RequestMapping("/subjectDetail")
    public Result subjectList(@RequestBody CommonRO ro) {
        Result re = new Result();
        List<SubjectItemVO> items = subjectService.findSubjectDetail(ro.getId());
        AppSubject subject = subjectService.findByid(ro.getId());
        setUrlOne(subject);
        for (SubjectItemVO item : items) {
        	if(item != null){
                item.initUrl(qiniuConfig.getURL());
        	}
        }

        re.putData("subject", subject);
        re.putData("items", items);
        return re;
    }

    /**
     * 商品搜索， 排序规则1 销量高到低 2 价格低到高 3价格高到低
     */
    @RequestMapping("/search")
    public Result search(@RequestBody ItemInfoSearchDto dto) {
        Result re = new Result();
        if (dto.getCurrentPage() == null) {
            dto.setPageSize(4);
            dto.setCurrentPage(1);
        }
        PageInfo<ItemInfoVO> pageInfo = itemService.finditemInfoByKey(dto);
//        for (ItemInfoVO vo : pageInfo.getList()) {
//            vo.initUrl(qiniuConfig.getURL());
//        }
//        Integer count = itemService.countitemInfoByKey(dto);
//        re.putData("total",count);
        re.putData("list", pageInfo);
        return re;
    }

    /**
     * 如果搜索无结果，则显示广告商品
     */
    @RequestMapping("/searchAd")
    public Result searchAdItem(@RequestBody BaseQueryDO baseDo) {
        Result re = new Result();
        if (baseDo == null || baseDo.getCurrentPage() == null) {
            baseDo = new ItemInfoSearchDto();
            baseDo.setPageSize(4);
            baseDo.setCurrentPage(1);
        }
        PageInfo<ItemInfoVO> list = itemService.findAdItem(baseDo);
        //Integer count = itemService.countAdItem();
        re.putData("list", list);
        return re;
    }

    /**
     * 系统logo
     */
    @RequestMapping("/sysLogo")
    public Result sysLoge() {
        Result re = new Result();
        re.putData("logo", qiniuConfig.getURL() + baseParaService.sysLoge());
        return re;
    }

}
